/*
 * @Author: xxp
 * @Date: 2020-08-14 17:38:13
 * @Last Modified by: xxp
 * @Last Modified time: 2020-08-19 13:46:14
 */
//小辉付部分公共业务
import { Toast, Modal } from "antd-mobile";
import yhTools from "./yh";
import { InterfaceG } from "../config/Config";
// 去提交授信
const alert = Modal.alert;
const submitCredit = function (lnid) {
	if (!lnid) {
		return;
	}
	let objs = {
		url: InterfaceG.submitCredit,
		params: {
			productNo: "601",
			lnid: lnid,
			userDeviceInfo: JSON.stringify(
				this.props.getAppPlatformsDatas.devicesInfo
			),
			version: "1.0"
		}
	};
	Toast.loading("加载中...", 100);
	yhTools.request(objs).then(res => {
		Toast.hide();
		// 0 未授信 1通过 2授信中 3重新授信 4授信拒绝
		if (res.code === "0") {
			const status = res.data.status;
			if (status == "1") {
				this.props.history.push({
					pathname: "/xhfHomeCredit"
				});
			} else if (status == "4") {
				this.props.history.push({
					pathname: "/xhfOpenFail",
					state: {
						message: res.data.message
					}
				});
			} else if (status == "2" || status == "3") {
				//授信中
				//科罗纳去10秒倒计时页面
				this.props.history.push("/xhfLivenessRet");
			}
		} else {
			Toast.info(res.message, 2);
		}
	});
};

//调查看额度接口
const seeAmount = function (longitude, latitude) {
	const _this = this;
	let objs = {
		url: InterfaceG.appBtnOpotion_new,
		params: {
			longitude: longitude,
			latitude: latitude,
			userDeviceInfo: JSON.stringify(
				this.props.getAppPlatformsDatas.devicesInfo
			),
			productNo: "601", // 代表小辉付
			version: "2.0"
		},
		_alertShow: false
	};
	Toast.loading("加载中...", 100);
	yhTools.request(objs).then(res => {
		_this.setState({
			maskShow: false
		});
		if (res.code === "0") {
			Toast.hide();
			const processNo = res.data.processNo;
			_this.props._getAppPlatformsDatas({
				lnid: res.data.lnid,
				applyNo: res.data.applyNo,
				processNo
			});

			if (res.data.lnid == 605) {
				// 已开通小晖贷 未开通小辉付 直接小辉付授信
				submitCredit.call(this, res.data.lnid);
			}
		} else {
			Toast.hide();
			Toast.info(res.message, 2);
		}
	});
};
//去设置允许打开地理位置
const openLoction = function () {
	yhTools.settingPrivacy();
};

const isHasLocation = function () {
	// console.log(this.props);
	let longitude = this.props.getAppPlatformsDatas.longitude || "";
	let latitude = this.props.getAppPlatformsDatas.latitude || "";
	if (!longitude || !latitude) {
		//获取经纬度
		window.YHAuthorize.callHandler("Privacy_Loaction", "", res => {
			// if (res.status == "1000") {
			const { data = {} } = res;
			longitude = data.longitude || "";
			latitude = data.latitude || "";
			this.props._getAppPlatformsDatas({
				longitude,
				latitude
			});
			seeAmount.call(this, longitude, latitude);
			// } else if (res.status == "3001") {
			// 	alert("", "请您打开手机定位权限，否则该功能无法正常使用", [
			// 		{
			// 			text: "取消",
			// 			onPress: () => console.log("cancel")
			// 		},
			// 		{
			// 			text: "设置",
			// 			onPress: () => openLoction()
			// 		}
			// 	]);
			// } else {
			// 	Toast.info(res.message, 2);
			// }
		});
	} else {
		seeAmount.call(this, longitude, latitude);
	}
};

export const xhfAuthAmount = function () {
	isHasLocation.call(this);
};

/**判断是否该跳转支付结果页 */
export const checkJumpPayResult = function ({ tradeNo, history }) {
	const objs = {
		url: InterfaceG.queryFinanceSuccessPage,
		params: {}
	};
	yhTools.request(objs).then(res => {
		if (res.code == "0") {
			const { flag } = res.data;
			if (flag) {
				sessionStorage.setItem("payResultTradeNo", tradeNo); //保存返回时能查询订单
				history.push(`/payResult`);
			} else {
				// 通知生活支付结果
				yhTools.noticePaySuccess("支付成功");
			}
		} else {
			// 通知生活支付结果
			yhTools.noticePaySuccess("支付成功");
		}
	});
};
